package com.openswimsoftware.orm.model.query.where;

import java.lang.reflect.Field;
import java.util.List;

import com.openswimsoftware.orm.ORM;

public class QueryWhere {
	
	private WhereCombo combo;
	private Field field;
	private WhereOperator operator;
	private String value;
	
	public QueryWhere(WhereCombo combo, Field field, WhereOperator operator, String value)
	{
		this.combo = combo;
		this.field = field;
		this.operator = operator;
		this.value = value;
	}
	
	public static String whereString(List<QueryWhere> queries)
	{
		if (queries == null)
		{
			return null;
		}
		
		String whereString = "";
		
		boolean firstQuery = true;
		for (QueryWhere query: queries)
		{
			if (firstQuery)
			{
				firstQuery = false;
			}
			else
			{
				whereString += " " + query.combo.toString() + " "; 
			}
			
			whereString += ORM.fieldName(query.field);
			whereString += " " + query.operator.toString() + " ?";
		}
		
		return whereString;
	}
	
	public static String[] whereArgs(List<QueryWhere> queries)
	{
		if (queries == null)
		{
			return null;
		}
		
		String[] whereArgs = new String[queries.size()];
		
		int i = 0;
		for (QueryWhere query: queries)
		{
			whereArgs[i++] = query.value;
		}
		
		return whereArgs;
	}

}
